<?php
/**
 * Created by PhpStorm.
 * User: RenBin
 * Date: 2015/12/1
 * Time: 16:05
 */

namespace common\models\Service;


use common\models\CommonFunction;
use common\models\DAO\DayAccrual;
use common\models\DAO\UserCompose;
use common\models\DAO\UserComposeAccrual;
use common\models\DAO\UserCoupon;
use common\models\DAO\UserCouponUseList;
use common\models\DAO\UserHongbao;
use common\models\DAO\UserHongbaoUseList;
use common\models\DAO\UserRate;
use common\models\DAO\UserWalletLog;
use common\msg\UserMsg;

class ComposeService {

    public static function deal($param){
        $userID = $param['userID'];
        $hongbao = intval(floatval($param['hongbao']) * 100);
        $integral = intval($param['integral']);
        $money = intval(floatval($param['money']) * 100);
        $coupon = intval($param['coupon']);
        if($hongbao <= 0 && $integral <= 0 && $money <=0 && $coupon <= 0){
            $result['msg'] = UserMsg::$no_compose;
            return $result;
        }
        if($integral > 0 && $integral < 100){
            $result['msg'] = '合成最少100积分！';
            return $result;
        }
        $card = 0;
        $user_remainder = Wallet::remainder($userID);
        $user_integral = MyAccount::integral($userID);

        $result = ['status'=>0, 'msg'=>UserMsg::$time_out];
        if($hongbao > 0){
            $user_hongbao = 0;
            $user_hongbao_model = UserHongbao::findOne($userID);
            $user_hongbao_model && $user_hongbao = intval($user_hongbao_model['money']);
            if($hongbao > $user_hongbao){
                $result['msg'] = UserMsg::$no_hongbao;
                return $result;
            }
        }

        if($integral > $user_integral){
            $result['msg'] = UserMsg::$no_integral;
            return $result;
        }

        if($money > $user_remainder){
            $result['msg'] = UserMsg::$no_money;
            return $result;
        }

        if($coupon > 0){
            $coupon_model = UserCoupon::findOne(['id'=>$coupon, 'userID'=>$userID, 'status'=>0]);
            if(!$coupon_model || date('Ymd') >= $coupon_model['loseTime']){
                $result['msg'] = UserMsg::$no_coupon;
                return $result;
            }
            $card = $coupon_model['card'];
        }

        $unit['hongbao'] = $hongbao / 100;
        $unit['integral'] = $integral / 100;
        $unit['money'] = $money;
        $unit['coupon'] = intval($card / 10 * 4);
        $transaction = \Yii::$app->db->beginTransaction();
        $now = time();

        $total_unit = array_sum($unit);
        //道具或者加息劵
        $_param = [['id'=>1, 'chance'=>60],//0.2 3
                  ['id'=>2, 'chance'=>40]//0.2 7
            ];
        $draw = DrawService::drawservice($_param);
        $type = 1;
        $compose_day = 0;
        if($draw == 1){
            $_param = [
                ['id'=>'0.2,3', 'chance'=>1550],//0.2 3
                ['id'=>'0.2,7', 'chance'=>1040],//0.2 7
                ['id'=>'0.2,15', 'chance'=>1040],//0.2 15
                ['id'=>'0.2,30', 'chance'=>1040],//0.2 30
                ['id'=>'0.3,3', 'chance'=>1040],//0.3 3
                ['id'=>'0.3,7', 'chance'=>1040],//0.3 7
                ['id'=>'0.3,15', 'chance'=>625],//0.3 15
                ['id'=>'0.3,30', 'chance'=>625],//0.3 30
                ['id'=>'0.5,3', 'chance'=>625],//0.5 3
                ['id'=>'0.5,7', 'chance'=>625],//0.5 7
                ['id'=>'0.5,15', 'chance'=>125],//0.5 15
                ['id'=>'0.5,30', 'chance'=>125],//0.5 30
                ['id'=>'1,3', 'chance'=>125],//1 3
                ['id'=>'1,7', 'chance'=>125],//1 7
                ['id'=>'1,15', 'chance'=>125],//1 15
                ['id'=>'1,30', 'chance'=>125],//1 30
            ];
            $total_unit >= 4000 && $_param = [
                    ['id'=>'0.5,3', 'chance'=>22],//0.5 3
                    ['id'=>'0.5,7', 'chance'=>20],//0.5 7
                    ['id'=>'0.5,15', 'chance'=>18],//0.5 15
                    ['id'=>'0.5,30', 'chance'=>15],//0.5 30
                    ['id'=>'1,3', 'chance'=>8],//1 3
                    ['id'=>'1,7', 'chance'=>7],//1 7
                    ['id'=>'1,15', 'chance'=>5],//1 15
                    ['id'=>'1,30', 'chance'=>3],//1 30
                ];
            $total_unit >= 4000 && $_param = [
                ['id'=>'0.5,3', 'chance'=>7],//0.5 3
                ['id'=>'0.5,7', 'chance'=>8],//0.5 7
                ['id'=>'0.5,15', 'chance'=>10],//0.5 15
                ['id'=>'0.5,30', 'chance'=>20],//0.5 30
                ['id'=>'1,3','chance'=>22],//1 3
                ['id'=>'1,7', 'chance'=>15],//1 7
                ['id'=>'1,15', 'chance'=>10],//1 15
                ['id'=>'1,30', 'chance'=>8],//1 30
            ];
            $type = 5;
            $draw_result = DrawService::drawservice($_param);
            $compose_coupon = explode(',', $draw_result);
            $compose_day = intval($compose_coupon[1]);
            $compose_count = intval(floatval($compose_coupon[0]) * 100);

        }else {
            $_param = [['id'=>8, 'chance'=>50],//0.2 3
                ['id'=>10, 'chance'=>30],//0.2 7
                ['id'=>11, 'chance'=>10],//0.2 7
                ['id'=>12, 'chance'=>5]];
            $result_compose = intval(DrawService::drawservice($_param)) / 10;
            arsort($unit);
            $unit_key = array_keys($unit);
            $frist = $unit_key[0];
            $compose_count = $total_unit * $result_compose;
            if ($money > 0) {
                $type = 4;
                $compose_count = intval($compose_count);
                $compose_count > $money * 1.5 && $compose_count = intval($money * 1.5);
            }else{
                if($frist == 'money')
                    $frist = $unit_key[1];
                switch ($frist) {
                    case 'hongbao':
                        $type = 3;
                        $compose_count = intval($compose_count * 100);
                        break;
                    case 'integral':
                        $compose_count = intval($compose_count * 100);
                        $type = 2;
                        break;
                    case 'coupon':
                        $type = 1;
                        $compose_count = intval($compose_count * 10 / 4);
                        $compose_count > 100 && $compose_count = 100;
                        break;
                }
            }
        }

        $composeModel = new UserCompose();
        $composeModel->userID = $userID;
        $composeModel->typeID = $type;
        $composeModel->count = $compose_count;
        $composeModel->createTime = $now;
        $composeModel->money = $money;
        $composeModel->hongbao = $hongbao;
        $composeModel->coupon = $card;
        $composeModel->couponID = $coupon;
        $composeModel->integral = $integral;
        $composeModel->day = $compose_day;
        $compose_result = $composeModel->save();
        if(!$compose_result){
            $transaction->rollBack();
            return $result;
        }
        $composeID = $composeModel->id;
        if($money > 0){
            $pub_param['userID'] = $userID;

            //更新用户余额 - 扣除账户余额
            //更新用户余额 - 扣除账户余额
            $accrual_total = DayAccrual::find()->where(array('userID'=>$userID))->andFilterWhere(['>', 'money', 0])->orderBy('date desc')->all();
            $init_money = $money;
            //钱包明细
            $user_wallet = new UserWalletLog();
            $user_wallet->type = 2;
            $user_wallet->money = $money;
            $user_wallet->createTime = $now;
            $user_wallet->userID = $userID;
            $user_wallet->road = 6;
            $user_wallet->gmID = $composeID;
            $user_wallet->desc = '合成';
            $user_wallet_result  = $user_wallet->save();
            if(!$user_wallet_result){
                $transaction->rollBack();
                return $result;
            }

            foreach($accrual_total as $v){
                $money = intval($v['money']);
                $id = $v['id'];
                if($money <= 0)
                    continue;
                if($money < $init_money){
                    $v->money = 0;
                    $v->updateTime = $now;
                    $one_result = $v->save();
                    if(!$one_result){
                        $transaction->rollBack();
                        return $result;
                    }
                    $compose_accrual = new UserComposeAccrual();
                    $compose_accrual->userID = $userID;
                    $compose_accrual->money = $money;
                    $compose_accrual->createTime = $now;
                    $compose_accrual->accrualID = $id;
                    $compose_accrual->composeID = $composeID;
                    $list_result = $compose_accrual->save();
                    if(!$list_result){
                        $transaction->rollBack();
                        return $result;
                    }
                    $init_money = $init_money - $money;
                }else{
                    $v->money = $money - $init_money;
                    $v->updateTime = $now;
                    $one_result = $v->save();
                    if(!$one_result){
                        $transaction->rollBack();
                        return $result;
                    }
                    $compose_accrual = new UserComposeAccrual();
                    $compose_accrual->userID = $userID;
                    $compose_accrual->money = $init_money;
                    $compose_accrual->createTime = $now;
                    $compose_accrual->accrualID = $id;
                    $compose_accrual->composeID = $composeID;
                    $list_result = $compose_accrual->save();
                    if(!$list_result){
                        $transaction->rollBack();
                        return $result;
                    }
                    break;
                }
            }

        }
        if($integral > 0){
            $deduct_param['road'] = 1;
            $deduct_param['composeID'] =  0;
            $deduct_param['desc'] = '合成';
            $deduct_param['userID'] = $userID;
            $deduct_param['integral'] = $integral;
            $deduct_param['createTime'] = $now;
            $integral_deduct = UserIntegralService::deduct($deduct_param);
            if(!$integral_deduct){
                $transaction->rollBack();
                return $result;
            }
        }
        if($hongbao > 0){
            $user_hongbao_model->money = $user_hongbao - $hongbao;
            $hb_result = $user_hongbao_model->save();
            if(!$hb_result){
                $transaction->rollBack();
                return $result;
            }
            $hb_use = new UserHongbaoUseList();
            $hb_use->userID = $userID;
            $hb_use->hongBao = $hongbao;
            $hb_use->createTime = $now;
            $hb_use->type = 3;
            $hb_use->composeID = $composeID;
            $hb_use_result = $hb_use->save();
            if(!$hb_use_result){
                $transaction->rollBack();
                return $result;
            }
        }

        //更新年利卷状态
        if($card > 0 ){
            $coupon_model->status = 1;
            $coupon_result = $coupon_model->save();
            if(!$coupon_result){
                $transaction->rollBack();
                return $result;
            }
            $coupon_use_model = new UserCouponUseList();
            $coupon_use_model->userID = $userID;
            $coupon_use_model->useTime = $now;
            $coupon_use_model->card = $card;
            $coupon_use_model->couponID = $coupon;
            $coupon_use_model->composeID = $composeID;
            $coupon_use_result = $coupon_use_model->save();
            if(!$coupon_use_result){
                $transaction->rollBack();
                return $result;
            }
        }
        if($type == 5){
            $userRate = new UserRate();
            $userRate->userID = $userID;
            $userRate->rate = $compose_count;
            $userRate->createTime = $now;
            $lose_day = $compose_day + 1;
            $userRate->loseTime = date('Ymd', strtotime("+$lose_day day"));
            $userRate->day = $compose_day;
            $userRate->composeID = $composeID;
            $rate_result = $userRate->save();
            if(!$rate_result){
                $transaction->rollBack();
                return $result;
            }
        }
        $date = date('Ymd');
        if($type == 4){
            //更新用户账号余额
            $dayAccrual = new DayAccrual();
            $dayAccrual->userID = $userID;
            $dayAccrual->date = $date;
            $dayAccrual->money = $compose_count;
            $dayAccrual->invest = $compose_count;
            $dayAccrual->createTime = $now;
            $dayAccrual->updateTime = $now;
            $dayAccrual->gmID = $composeID;
            $dayAccrual->typeID = 16;
            $accrual_result  = $dayAccrual->save();
            if(!$accrual_result){
                $transaction->rollBack();
                return $result;
            }
            //钱包明细
            $user_wallet = new UserWalletLog();
            $user_wallet->type = 1;
            $user_wallet->money = $compose_count;
            $user_wallet->createTime = $now;
            $user_wallet->userID = $userID;
            $user_wallet->road = 16;
            $user_wallet->desc = '炼金';
            $user_wallet_result = $user_wallet->save();
            if(!$user_wallet_result){
                $transaction->rollBack();
                return $result;
            }
            if(!empty($param['openID'])){
                SendWxMessage::charge($param['openID'], CommonFunction::formatMoney($compose_count), '炼金成功！');
            }
        }
        if($type == 3){
            $hongbao_param['hongBao'] = $compose_count;
            $hongbao_param['userID'] = $userID;
            $hongbao_param['createTime'] = $now;
            isset($param['openID']) && $hongbao_param['openID'] = $param['openID'];
            $hongbao_param['composeID'] = $composeID;
            $hongbao_param['desc'] = '炼金获得'.CommonFunction::formatMoney($compose_count).'现金红包';
            $hongbao_result = HongBaoService::acquire($hongbao_param);
            if(!$hongbao_result){
                $transaction->rollBack();
                return $result;
            }
        }
        if($type == 2){
            $integral_data['recID'] = $userID;
            $integral_data['type'] = 10;
            $integral_data['integral'] = $compose_count;
            $integral_data['des'] = '炼金';
            $integral_data['composeID'] = $composeID;
            isset($param['openID']) && $integral_data['openID'] = $param['openID'];
            $integral_result = IntegralService::addIntegral($integral_data);
            if(!$integral_result){
                $transaction->rollBack();
                return $result;
            }
        }

        if($type == 1){
            $user_coupon = new UserCoupon();
            $user_coupon->userID = $userID;
            $user_coupon->createTime = $now;
            $user_coupon->card = $compose_count;
            $lose_day = 31 + 1;
            $user_coupon->loseTime = date('Ymd', strtotime("+$lose_day day"));
            $user_coupon->composeID = $composeID;
            $user_coupon_result = $user_coupon->save();
            if(!$user_coupon_result){
                $transaction->rollBack();
                return $result;
            }
        }

        $transaction->commit();
        if($type != 2)
            $compose_count = CommonFunction::formatMoney($compose_count);
        $result = ['status'=>1, 'type'=>$type, 'count'=>$compose_count];
        $remainder = Wallet::remainder($userID);
        $hongbao = HongBaoService::get($userID);
        $integral = MyAccount::integral($userID);
        $result['show'] = [0=>CommonFunction::formatMoney($hongbao), 1=>$integral,2=> CommonFunction::formatMoney($remainder)];
        $result['attr'] = [$hongbao / 100, $integral, $remainder / 100];
        $result['coupon'] = CouponService::get($userID);
        return $result;
    }
}